package mhl.view;

import mhl.domain.*;
import mhl.service.BillService;
import mhl.service.DiningTableService;
import mhl.service.EmployeeService;
import mhl.service.MenuService;
import mhl.utils.Utility;

import java.util.List;

/**
 * 满汉楼饭店管理主界面
 */
public class MHLView {

    private boolean loop = true;//控制是否退出菜单
    private String key = "";//接收用户的收入
    //定义 EmployeeService 属性
    private EmployeeService employeeService = new EmployeeService();
    //定义 DiningTableService 属性
    private DiningTableService diningTableService = new DiningTableService();
    //定义 MenuService 属性
    private MenuService menuService = new MenuService();
    //定义 BillService 属性
    private BillService billService = new BillService();

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    //完成结账
    public void payBill() {
        while (true) {
            System.out.print("请选择要结账的餐桌编号(-1退出): ");
            int diningTableId = Utility.readInt();
            if (diningTableId == -1) {
                System.out.println("已取消结账~");
                return;
            }
            while (true) {
                //验证餐桌是否存在
                DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
                if (diningTable == null) {
                    System.out.println("结账的" + diningTableId + "号餐桌不存在");
                    break;
                }
                //验证餐桌是否有需要结账的账单
                if (!billService.hasPayBillByDiningTableId(diningTableId)) {
                    System.out.println(diningTableId + "号餐桌没有未结账的账单");
                    return;
                }
                System.out.print("请选择结账方式(现金/支付宝/微信)回车表示退出: ");
                String payMode = Utility.readString(20, "");//如果回车，就是返回 ""
                if (payMode.equals("")) {
                    System.out.println("已取消结账~");
                    return;
                }
                char key = Utility.readConfirmSelection();
                if (key == 'Y') { //结账
                    if (billService.payBill(diningTableId, payMode)) {
                        System.out.println(diningTableId + "号餐桌结账成功~");
                    } else {
                        System.out.println("结账失败~");
                    }
                } else {
                    System.out.println("已取消结账~");
                }
                return;
            }

        }

    }

    //显示账单信息
    public void showBillS() {
//        List<Bill> bills = billService.getBills();
//        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
//        for (Bill bill : bills) {
//            System.out.println(bill);
//        }
//        System.out.println("所有的账单显示完毕~");

        //显示账单信息并包含菜品名
        List<MultiTableBean> multiTableBeans = billService.getBills2();
        System.out.println("\n编号\t\t菜品号\t菜品名\t\t单价\t\t\t菜品量\t\t金额\t\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
        for (MultiTableBean bill : multiTableBeans) {
            System.out.println(bill);
        }
        System.out.println("所有的账单显示完毕~");


    }


    //完成点餐
    public void orderMenu() {
        while (true) {
            System.out.print("请输入点餐的桌号(-1退出): ");
            int orderDiningTableId = Utility.readInt();
            if (orderDiningTableId == -1) {
                System.out.println("已退出点餐~");
                return;
            }
            while (true) {
                //验证餐桌号是否存在
                DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
                if (diningTable == null) {
                    System.out.println(orderDiningTableId + "号的餐桌不存在~");
                    break;
                }
                while (true) {
                    System.out.print("请输入点餐的菜品号(-1退出): ");
                    int orderMenuId = Utility.readInt();
                    if (orderMenuId == -1) {
                        System.out.println("已退出点餐~");
                        return;
                    }
                    //验证菜品编号
                    Menu menu = menuService.getMenuById(orderMenuId);
                    if (menu == null) {
                        System.out.println(orderMenuId + "号的菜品不存在~");
                        break;
                    }

                    System.out.print("请输入点餐的菜品量(-1退出): ");
                    int orderNums = Utility.readInt();
                    if (orderNums == -1) {
                        System.out.println("已退出点餐~");
                        return;
                    }
                    //点餐
                    if (billService.orderMenu(orderMenuId, orderNums, orderDiningTableId)) {
                        System.out.println("点餐成功~");
                    } else {
                        System.out.println("点餐失败~");
                    }
                    return;
                }
            }
        }

    }

    //显示所有的菜品
    public void showMenuS() {
        List<Menu> list = menuService.getMenus();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
        for (Menu menu : list) {
            System.out.println(menu);
        }
        System.out.println("所有的菜品显示完毕~");
    }

    //完成预订餐桌
    public void orderDiningTable() {
        System.out.print("请选择要预定的餐桌编号(-1退出): ");
        int orderId = Utility.readInt();
        if (orderId == -1) {
            System.out.println("退出了餐桌预订。");
            return;
        }
        //根据 orderId 返回对应的 DiningTable对象，如果为null，则说明对象不存在
        DiningTable diningTable = diningTableService.getDiningTableById(orderId);
        if (diningTable == null) {
            System.out.println("预订的餐桌编号不存在。");
            return;
        }
        //该方法得到的是 Y 或者 N,来确认是否预订
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {
            //判断该餐桌的状态是否为 空
            if (!(diningTable.getState().equals("空"))) {//说明当前这个餐桌正被使用，不是 "空" 状态
                System.out.println("该餐桌正被使用或已被预订。");
                return;
            }
            //接收预定信息
            System.out.print("预定人的名字: ");
            String orderName = Utility.readString(50);
            System.out.print("预定人的电话: ");
            String orderTel = Utility.readString(50);

            //更新餐桌状态
            if (diningTableService.orderDiningTable(orderId, orderName, orderTel)) {
                System.out.println("已成功预订编号为[" + orderId + "]的餐桌!");
            } else {
                System.out.println("编号为[" + orderId + "]的餐桌预订失败");
            }

        } else {
            System.out.println("取消预订餐桌");
        }
    }


    //显示所有餐桌状态
    public void showDiningTableState() {
        List<DiningTable> list = diningTableService.getDiningTables();
        System.out.println("\n餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("餐桌状态显示完毕~");
    }

    //显示主菜单
    public void mainMenu() {
        while (loop) {
            System.out.println("\n================满汉楼================");
            System.out.println("\t\t\t1 登录满汉楼");
            System.out.println("\t\t\t2 退出满汉楼");
            System.out.print("请输入你的选择: ");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("输入你的员工号: ");
                    String empId = Utility.readString(50);
                    System.out.print("输入你的密码: ");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);
                    //到数据库去判断
                    if (employee != null) {//该用户存在
                        System.out.println("===============登录成功[" + employee.getName() + "]===============\n");
                        //显示二级菜单, 这里二级菜单是循环操作，所以做成while
                        while (loop) {
                            System.out.println("\n================满汉楼(二级菜单)================");
                            System.out.println("\t\t\t1 显示餐桌状态");
                            System.out.println("\t\t\t2 预定餐桌");
                            System.out.println("\t\t\t3 显示所有的菜品");
                            System.out.println("\t\t\t4 点餐服务");
                            System.out.println("\t\t\t5 查看账单");
                            System.out.println("\t\t\t6 结账");
                            System.out.println("\t\t\t9 退出满汉楼");
                            System.out.print("请输入你的选择: ");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    showDiningTableState();//显示所有餐桌状态
                                    break;
                                case "2":
                                    System.out.println("=================预定餐桌=================");
                                    showDiningTableState();
                                    orderDiningTable();//预定餐桌
                                    break;
                                case "3":
                                    showMenuS();//显示所有的菜品
                                    break;
                                case "4":
                                    System.out.println("=================点餐服务=================");
                                    showDiningTableState();
                                    showMenuS();
                                    orderMenu();//点餐
                                    break;
                                case "5":
                                    showBillS();//查看账单
                                    break;
                                case "6":
                                    System.out.println("=================结账服务=================");
                                    showBillS();
                                    payBill();//结账
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("你的输入有误，请重新输入: ");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("登入失败~");
                    }
                    break;
                case "2":
                    loop = false;//退出满汉楼系统
                    break;
                default:
                    System.out.println("你的输入有误，请重新输入: ");

            }

        }
        System.out.println("已成功退出满汉楼管理系统~");
    }
}
